package jmeter.card.online;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.asiainfo.vc.card.service.VCCardService;

import tool.MyTime;

/**
 * 1.2.8	退费制卡售卡接口
 */
public class RefundCardService extends AbstractJavaSamplerClient {
	
	public static final Log log = LogFactory.getLog(RefundCardService.class);
	private static final ClassPathXmlApplicationContext cont = new ClassPathXmlApplicationContext(
			"application_consumer.xml");
	public VCCardService vc;
	private String encoding = "UTF-8";
	Map<String, Object> inMap = new HashMap<String, Object>();
	Map<String, Object> outMap = new HashMap<String, Object>();

	// 3.设置页面默认参数
	@Override
	public Arguments getDefaultParameters() {
		Arguments parame = new Arguments();
		//请求流水满足集团规范。
		parame.addArgument("requestId", "8057");
		//CRM的标识或IP地址 
		parame.addArgument("requestSource", "134.96.102.181");
		//CRM登录用户名或者营业员工号码
		parame.addArgument("requestUser", "crm_login");
		//请求时间，YYYYMMDDHHMMSS
		parame.addArgument("requestTime", "YYYYMMDDHHMMSS");
		//充值接入途径（1：IVR； 2：SMS； 3： 网上营业厅即WEB方式；4：WAP；5：USSD；6：CRM）
		parame.addArgument("accessType", "6");
		//充值卡卡号。12～20位的数字字符串。
		parame.addArgument("cardNo", "5711005045860888899");
		//退费金额
		parame.addArgument("balance", "1999");
		//卡的有效期YYYYMMDD
		parame.addArgument("expireTime", "YYYYMMDD");
		//附加有效期 
		parame.addArgument("prolongdays", "720");

		return parame;
	}

	@Override
	public SampleResult runTest(JavaSamplerContext context) {
		SampleResult result = new SampleResult();
		vc = (VCCardService) cont.getBean("vcCardService");
		// 设置请求参数到map中
		inMap.put("requestId", MyTime.getRandom("")+context.getParameter("requestId"));
		inMap.put("requestSource", context.getParameter("requestSource"));
		inMap.put("requestUser", context.getParameter("requestUser"));
		inMap.put("requestTime", MyTime.gettime(""));
		inMap.put("accessType", context.getIntParameter("accessType"));
		inMap.put("cardNo", context.getParameter("cardNo"));
		inMap.put("balance", context.getIntParameter("balance"));
		inMap.put("expireTime", MyTime.gettime("am"));
		inMap.put("prolongdays", context.getIntParameter("prolongdays"));
		System.err.println(inMap);
		// 设置统计时间开始点
		result.sampleStart();
		try {
			outMap = vc.refundCardService(inMap);
			if ("0".equals(String.valueOf(outMap.get("result")))) {
				result.setResponseCode(String.valueOf(outMap.get("result")));
				result.setResponseMessage(String.valueOf(outMap.get("desc")));
				result.setSamplerData(String.valueOf(inMap));
				result.setResponseData(String.valueOf(outMap), encoding);
				result.setSuccessful(true);
			} else {
				result.setResponseCode(String.valueOf(outMap.get("result")));
				result.setResponseMessage(String.valueOf(outMap.get("desc")));
				result.setSamplerData(String.valueOf(inMap));
				result.setResponseData(String.valueOf(outMap), encoding);
				result.setSuccessful(false);
			}

		} catch (Exception e) {
			result.setSamplerData(String.valueOf(inMap));
			result.setResponseData("", encoding);
			result.setSuccessful(false);
			e.printStackTrace();
		} finally {
			result.sampleEnd();
		}
		return result;
	}

	public static void main(String[] args) {
		RefundCardService jqr = new RefundCardService();
		JavaSamplerContext jsc = new JavaSamplerContext(jqr.getDefaultParameters());
		// 需要手动调用,先调用开在运行在结束
		SampleResult sr = jqr.runTest(jsc);
		System.err.println(sr.getResponseCode());
		System.err.println(sr.getResponseMessage());
		System.err.println(sr.getSamplerData());
		System.err.println(sr.getResponseDataAsString());
	}
}
